<?php

namespace app\controller\admin\ah;

use app\model\api\ah\AhCoordinate;
use app\model\api\ah\AhEquipment;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Coordinate
 * @package app\controller\admin\ah
 */
class Coordinate extends Backend
{
    protected $model;
    public function initialize()
    {
        $this->model = new AhCoordinate();
        (new AhCoordinate())->startTrans();
        parent::initialize(); // TODO: Change the autogenerated stub
    }


    public function add()
    {
        $equipment_model = new AhEquipment();
        $post     = CommonFun::filterPostData($this->request->post());
        if($post['kind8-1'] == $post['kind8-2']){
            return $this->error("套装两个戒指不能一样");
        }
        $kindList = $this->model->getKindList();
        $ids = [];
        foreach ($kindList as $item){
            if(isset($post[$item])&&$post[$item]!=''){
                array_unshift($ids,$post[$item]);
            }
        }
        if(count($ids)>0){
            $count = $equipment_model->whereIn('id',$ids)->whereNotNull('coordinate_id')->count();
            if($count>0){
                return $this->error('您选择的装备已存在别的套装中，请刷新数据重新添加');
            }
        }
        $insData = [
            'name' => $post['name'],
            'coordinate_id' => 0,
            'attr1' => $post['attr1'],
            'attr2' => $post['attr2'],
            'attr3' => $post['attr3'],
        ];
        try {

            $save = $this->model->save($insData);
            $id = $this->model->getLastInsID();
            $this->model->where('id',$id)->update(['coordinate_id'=>$id]);
            if (!$save) throw new \Exception('保存失败');
            if(count($ids)>0){
                $equipment_model->whereIn('id',$ids)->update(["coordinate_id"=>$id]);
            }
            $this->model->commit();
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {

            return $this->error($e->getMessage());
        }
    }


    public function edit()
    {
        $equipment_model = new AhEquipment();
        $post     = CommonFun::filterPostData($this->request->post());
        if($post['kind81'] == $post['kind82']){
            return $this->error("套装两个戒指不能一样");
        }
        $kindList = $this->model->getKindList();
        $ids = [];
        foreach ($kindList as $item){
            if(isset($post[$item])&&$post[$item]!=''){
                array_unshift($ids,$post[$item]);
            }
        }
        if(count($ids)>0){
            $count = $equipment_model->whereIn('id',$ids)->whereNotIn('coordinate_id',[$post['id']])->whereNotNull('coordinate_id')->count();
            if($count>0){
                return $this->error('您选择的装备已存在别的套装中，请刷新数据重新添加');
            }
        }
        $detail = $this->model->find($post['id']);
        empty($detail) && $this->error('数据不存在');
        $detail->name = $post['name'];
        $detail->attr1 =$post['attr1'];
        $detail->attr2 =$post['attr2'];
        $detail->attr3 =$post['attr3'];
        try {

            $save = $detail->save();
            if (!$save) throw new \Exception('保存失败');
            if(count($ids)>0){
                $equipment_model->where('coordinate_id','=',$post['id'])->update(["coordinate_id"=>'']);
                $equipment_model->whereIn('id',$ids)->update(["coordinate_id"=>$post['id']]);
            }
            $this->model->commit();
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {

            return $this->error($e->getMessage());
        }
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->with('equipments')->find($id);
        if(is_object($info))$info = $info->toArray();
        if($info){
            $i=1;
            foreach ($info['equipments'] as $item){
                if(isset($item['kind'])){
                    if($item['kind'] == 8){
                        $item['kind'] = $item['kind'].$i;
                        $i++;
                    }
                    $info["kind".$item['kind']] = $item['id'];
                }
            }
        }
        return $this->success('获取成功', $info);
    }


    //删除
    public function del()
    {
        $ids = array_filter($this->request->post('ids'));
        if (!$ids) {
            return $this->error('参数ids不能为空');
        }
        try{
            if ($this->model->destroy($ids)) {
                (new AhEquipment())->whereIn('coordinate_id',$ids)->update(['coordinate_id'=>null]);
                $this->clearCache();
                $this->model->commit();
                return $this->success('数据删除成功');
            } else {
                return $this->error('数据删除失败');
            }
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }
}